//
//  ChangePasswordSecondViewController.m
//  huashiedu
//
//  Created by oceans on 2018/9/12.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "ChangePasswordSecondViewController.h"
#import "Utility.h"
#import "TipManager.h"

@interface ChangePasswordSecondViewController ()

@property (strong, nonatomic) UITextField *username;
@property (strong, nonatomic) UITextField *password;
@end

@implementation ChangePasswordSecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
}

- (void)setupView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"密码修改";
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [self.view addGestureRecognizer:tap];
    
    self.username = [[UITextField alloc] initWithFrame:CGRectMake(31, XNavigationHeight + 30, ScreenWidth - 62, 35)];
    _username.placeholder = @"请输入6-20个英文字母、数字";
    _username.secureTextEntry = YES;
    [self.view addSubview:_username];
    
    CALayer *topline = [[CALayer alloc] init];
    topline.frame = CGRectMake(30, CGRectGetMaxY(_username.frame) + 6.5, ScreenWidth - 60, 0.5);
    topline.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1.0].CGColor;
    [self.view.layer addSublayer:topline];
    
    self.password = [[UITextField alloc] initWithFrame:CGRectMake(31, CGRectGetMaxY(topline.frame) + 17, ScreenWidth - 62, 35)];
    _password.placeholder = @"确认新密码";
    _password.secureTextEntry = YES;
    [self.view addSubview:_password];
    
    CALayer *botline = [[CALayer alloc] init];
    botline.frame = CGRectMake(30, CGRectGetMaxY(_password.frame) + 6.5, ScreenWidth - 60, 0.5);
    botline.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1.0].CGColor;
    [self.view.layer addSublayer:botline];
    
    UIButton *login = [UIButton buttonWithType:UIButtonTypeCustom];
    login.frame = CGRectMake(31, CGRectGetMaxY(botline.frame) + 35.0, ScreenWidth - 62, 50);
    login.backgroundColor = COLOR(253, 119, 77, 1);
    [login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    login.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [login setTitle:@"确定" forState:UIControlStateNormal];
    [login addTarget:self action:@selector(done) forControlEvents:UIControlEventTouchUpInside];
    login.layer.cornerRadius = 3.0;
    login.layer.masksToBounds = YES;
    [self.view addSubview:login];
}

- (void)hideKeyboard {
    [self.view endEditing:YES];
}

- (void)done {
    
    if (_username.text.length > 0 && [_username.text isEqualToString:_password.text]) {
        
        if ([Utility checkPassword:_username.text]) {
            __weak typeof(self) weakself = self;
            [[UserModel shared] resetPassword:_password.text successBlock:^{
                [TipManager showTip:@"修改成功"];
                [weakself.navigationController popToRootViewControllerAnimated:YES];
            }];
        }else {
            [TipManager showTip:@"输入的密码无法识别"];
        }
    }else {
        [TipManager showTip:@"请输入相同的密码"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
